package com.cykj.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Program: Mooc-Admin
 * @Package: com.cykj.util
 * @Author: 张俊杰
 * @Create: 2025-07-08 22:56
 * @Description: TODO
 * @Vision: 1.0
 **/

@Component
public class RedisServiceUtil {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 存值
    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    // 存值并设置过期时间（单位：秒）
    public void setWithExpire(String key, Object value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    // 取值
    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 删除值
    public void delete(String key) {
        redisTemplate.delete(key);
    }
}
